package demo3;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class TestConnection {
    private static final String URL = "jdbc:mysql://localhost:3306/usersDb";

    private static final String USERNAME = "root";

    private static final String PASSWORD = "123456";

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("欢迎来到注册系统");
        System.out.println("请输入用户名");
        String username = scanner.next();
        System.out.println("请输入密码");
        String password = scanner.next();
//0、idea 引入AI编程工具，通义必的
        //一、需求：完成用户册
//1.连接数据库：urL（主机号 端口号 数拟库名称）用户名密码。
        Class.forName("com.mysql.cj.jdbc.Driver");

        Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
//2.编写sqL语句
        String sql = "insert into users(username, password) values(?, ?)";
        // 3.执行sqL
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        preparedStatement.setString(1, username);
        preparedStatement.setString(2, password);
        int i = preparedStatement.executeUpdate();
        if (i > 0) {
            System.out.println("注册成功");
        } else {
            System.out.println("注册失败");

        }
    }
}


